#!/usr/bin/env bash

echo 'start aurelia starting'

# 阿拉伯数字18作为异常退出码
check='[ $? -eq 0 ] || exit 18'

eval "$check" && echo '进入 min 目录'
eval "$check" && cd min
process_name='serve -s dist -p 14001'
process_cmd="ps ax|grep '$process_name'|grep -v grep|awk '{print $1}'"
old_pid=`eval $process_cmd`
echo "aurelia old_pid=${old_pid}"
if [ -z "$old_pid" ];then
  echo "aurelia 服务之前未重启过!"
else
  echo "aurelia 关闭旧的进程!"
  kill -9 ${old_pid}
  mid_pid=`eval $process_cmd`
  if [ -z "${mid_pid}" ];then
      echo "aurelia 进程关闭成功！"
  else
      echo "aurelia 进程关闭失败，"
      exit 18
  fi
fi
eval "$check" && echo '启动服务 aurelia'
eval "$check" && nohup $process_name >> aurelia.log &

eval "$check" && echo 'finish aurelia starting'